package com.mgrid.convert.clickhouse.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mgrid.convert.clickhouse.dao.HistorysignalMapper;
import com.mgrid.convert.clickhouse.entity.Historysignal;
import com.mgrid.convert.clickhouse.service.HistorysignalService;
import com.mgrid.convert.mysql.entity.SHistorysignal;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;


/**
 *
 */
@Service
public class HistorysignalServiceImpl extends ServiceImpl<HistorysignalMapper, Historysignal>
    implements HistorysignalService {

    @Resource
    com.mgrid.convert.mysql.service.SHistorysignalService SHistorysignalService;

    @Resource
    HistorysignalService historysignalService;

    @Override
    @DS("clickhouse")
    public void sqlConvert() {
        SimpleDateFormat nowDate = new SimpleDateFormat("yyyy-MM-dd");
        String currentTime = nowDate.format(new Date()) + " 00:00:00";
        List<Historysignal> historysignals = this.baseMapper.listAll(currentTime);
        if(historysignals.size() != 0){
            SHistorysignal sHistorysignal = new SHistorysignal();
            for (Historysignal historysignal : historysignals) {
                Integer roomid = historysignal.getRoomid();
                String roomname = historysignal.getRoomname()+"";
                Integer samplerid = historysignal.getSamplerid();
                String samplername = historysignal.getSamplername()+"";
                Integer equipmentid = historysignal.getEquipmentid();
                String equipmentname = historysignal.getEquipmentname()+"";
                Integer signalid = historysignal.getSignalid();
                String signalname = historysignal.getSignalname()+"";
                Date recordtime = historysignal.getRecordtime();
                Double floatvalue = historysignal.getFloatvalue();

                Random random = new Random();
                long currentTimeMillis = System.currentTimeMillis();
                String id = String.format("%d%d", currentTimeMillis, random.nextLong());

                sHistorysignal.setId(id);
                sHistorysignal.setRoomid(roomid);
                sHistorysignal.setRoomname(roomname);
                sHistorysignal.setSamplerid(samplerid);
                sHistorysignal.setSamplername(samplername);
                sHistorysignal.setEquipmentid(equipmentid);
                sHistorysignal.setEquipmentname(equipmentname);
                sHistorysignal.setSignalid(signalid);
                sHistorysignal.setSignalname(signalname);
                sHistorysignal.setRecordtime(recordtime);
                sHistorysignal.setFloatvalue(floatvalue);
                SHistorysignalService.save(sHistorysignal);
            }
        }
    }
}




